﻿function relatehover(id) {
    $(id).addClass('hover');
}
function relateout(id) {
    $(id).removeClass('hover');
}
function showhideQuickview(obj) {

    if ($(obj).attr('id') == $('.related_deal .selected').attr('id')) {
        $('.details-pane').hide();
        $(obj).removeClass('selected');
        $(obj).find('.recent_box_arrow').removeClass('selected');
    }
    else {

        $('.recent_deal').removeClass('selected');
        $('.recent_box_arrow').removeClass('selected');

        $(obj).addClass('selected');
        $(obj).find('.recent_box_arrow').addClass('selected');

        loadDetails($(obj).attr('dealid'));
        $('.details-pane').show();
    }


    rePositionRightDetailPanel_Scroll();
}

function rePositionRightDetailPanel_Scroll() {

    var _borderTop = $('#main_right').offset().top + 4;
    var _anchorTop = $('.details-pane-top-anchor').offset().top + 2;

    var _objTop;
    if (_borderTop > _anchorTop)
        _objTop = _borderTop - _anchorTop;
    else _objTop = 0;
    // khong cach tu dau box toi diem dung dau 


    var _left = $('#main_left').offset().left + 688;
    var _height = $(window).height();
    var _bottom = $('.details-pane-bottom-anchor').offset().top;


    if (_anchorTop + _height > _bottom) {
        if (_bottom > _anchorTop)
            _height = _bottom - _anchorTop;
        else
            _height = 0;
    }
    if (_anchorTop < _borderTop) {
        var $height1 = _height;
        _height = $height1 - (_borderTop - _anchorTop);
    }

    var _width = $(window).width();
    if (_width < 990) {
        _left = 990 - 300;
    }

    $('.details-pane').css('top', _objTop.toString() + 'px')
        .css('left', _left.toString() + 'px')
        .css('height', _height.toString() + 'px');

    if (_height >= 40)
        $('.pane-components').css('height', (_height - 40).toString() + 'px');
    $('.pane-components').jScrollPane({ showArrows: false, scrollbarWidth: 10 });
    $('.pane-components').css('height', (_height - 40).toString() + 'px');
    $('.jScrollPaneContainer').css('height', (_height - 20).toString() + 'px').css('width:334px;');
}

function InitScript() {

    $('.recent_deal').click(function() {
        showhideQuickview(this);
    }).hover(function() {
        $(this).addClass('hover');
    },
	function() {
	    $(this).removeClass('hover');
	});

	$('.recent_supplier').hover(function() {
	    $(this).addClass('hover');
	},
	function() {
	    $(this).removeClass('hover');
	});

	$('.viewFullCategory').click(function() {
	    $(this).parent().find('[class^=related_]:gt(2)').toggle();
	});

	$('.viewAllSub').click(function() {
	    $(this).parent().find('[class^=subCatLink]:gt(6)').toggle();
	});
    
    var $rotateImage;
    $rotateImage = 1;

    $(window).resize(function() {
        if ($('.details-pane:visible').attr('class') == 'details-pane')
            rePositionRightDetailPanel_Scroll();
    }).scroll(function() {
        if ($('.details-pane:visible').attr('class') == 'details-pane')
            rePositionRightDetailPanel_Scroll();
    });

    $('.stream_item').click(function() {
        /*-- If current stream is selected -> remove detail panel & remove 'selected_stream' class --*/
        if ($(this).attr('class') == $('.hover_arrow').attr('class')) {
            $('.details-pane').hide();
            $(this).removeClass('hover_arrow');
        }
        else {
            /*-- Else add 'selected_stream' for current stream, remove class 'selected_stream' from current selected stream.--*/
            $(this).parent().find('.hover_arrow').removeClass('hover_arrow');
            $(this).addClass('hover_arrow');
            loadDetailContent($(this));
            $('.details-pane').show();
            rePositionRightDetailPanel_Scroll();

        }
    })

    $('.pane-close').click(function() {
        $('.hover_arrow').removeClass('hover_arrow');
        $('.details-pane').hide();
        $('.recent_deal').removeClass('selected');
    })

    if ($('.details-pane:visible').attr('class') == 'details-pane')
        rePositionRightDetailPanel_Scroll();
}

function CountV3(id) {
    var $countdown = $('.remaining_time_container' + id.toString() + ':visible').attr('id');
    if ($countdown) {
        $currentTime = new Date($('.listdealCurrentTime').attr('value').toString());
        $timeLV = 4;
        var $timecount = $('.js_timecount' + id.toString() + '');
        if ($timecount != null)
            if ($timecount.attr('value') != null) {
            $deadtime = new Date($timecount.attr('value').toString());
            var $period = $deadtime - $currentTime;
            $day = parseInt($period / 3600000 / 24);
            if ($day > 0) { $('.HD_days' + id.toString() + ' .timecount').text($day.toString()); }
            else if ($timeLV == 4) { $timeLV = 3; $('.HD_days' + id.toString() + '').hide(); }
            $period = $period - $day * 3600000 * 24;
            $hour = parseInt($period / 3600000);
            if ($hour + $day > 0) { $('.HD_hours' + id.toString() + ' .timecount').text($hour.toString()); }
            else if ($timeLV == 3) { $timeLV = 2; $('.HD_hours' + id.toString() + '').hide(); }
            $period = $period - $hour * 3600000;
            $min = parseInt($period / 60000);
            if ($hour + $day + $min > 0) { $('.HD_minutes' + id.toString() + ' .timecount').text($min.toString()); }
            else if ($timeLV == 2) { $timeLV = 1; $('.HD_minutes' + id.toString() + '').hide(); }
            $period = $period - $min * 60000;
            $sec = parseInt($period / 1000);
            if ($sec + $day + $hour + $min > 0) { $('.HD_seconds' + id.toString() + ' .timecount').text($sec.toString()) }
            else if ($timeLV == 1) {                
                $timeLV = 0;
                $('.remaining_time_container' + id.toString() + ' .time p').text('Phá giá kết thúc vào');
                var hh = parseInt($deadtime.getHours().toString());
                var mm = parseInt($deadtime.getMinutes().toString());
                $('.HD_seconds' + id.toString() + '').html(((hh > 12) ? ((hh - 12).toString()) : hh.toString()) + ':' + (mm > 10 ? mm.toString() : '0' + mm.toString()) + (hh > 12 ? ' PM' : ' AM') + '<br/>' + $deadtime.getDate().toString() + '/' + ($deadtime.getMonth() + 1).toString() + '/' + $deadtime.getFullYear().toString());
                $('.remaining_time_container' + id.toString() + ' img').attr('src', '/images/groupon/icons/hourglass/hourglassfinal.gif');
                $('.remaining_time_container' + id.toString()).parent().find('.imgBtBuy').attr('src', '/images/groupon/buttons/buy_expire.jpg').attr('alt', 'Hết hạn phá giá').click(function() { return false; });
                return;
            }
            doCountDownV3(id);
        }
    }
}

function CountV3PP(id) {

    var $countdown = $('.js_timecount' + id.toString()).attr('value');
    if ($countdown) {
        $currentTime = new Date($('.listdealCurrentTime').attr('value').toString());
        $timeLV = 4;
        var $timecount = $('.js_timecount' + id.toString() + '');
        if ($timecount != null)
            if ($timecount.attr('value') != null) {
            $deadtime = new Date($timecount.attr('value').toString());
            var $period = $deadtime - $currentTime;
            $day = parseInt($period / 3600000 / 24);
            if ($day > 0) {convertPart('day_' + id.toString() + '_', $day);}
            else if ($timeLV == 4) { $timeLV = 3; convertPart('day_' + id.toString() + '_', 0); }
            $period = $period - $day * 3600000 * 24;
            $hour = parseInt($period / 3600000);
            if ($hour + $day > 0) { convertPart('hour_' + id.toString() + '_', $hour); }
            else if ($timeLV == 3) { $timeLV = 2; convertPart('day_' + id.toString() + '_', 0); }
            $period = $period - $hour * 3600000;
            $min = parseInt($period / 60000);
            if ($hour + $day + $min > 0) { convertPart('min_' + id.toString() + '_', $min); }
            else if ($timeLV == 2) { $timeLV = 1; convertPart('min_' + id.toString() + '_', 0) }
            $period = $period - $min * 60000;
            $sec = parseInt($period / 1000);
            if ($sec + $day + $hour + $min > 0) { convertPart('sec_' + id.toString() + '_', $sec) }
            else if ($timeLV == 1) {
                $timeLV = 0;
                $('.js_timecount' + id.toString()).parent().parent().find('.imgBtBuy').attr('src', '/images/groupon/buttons/buy_expire.jpg').attr('alt', 'Hết hạn phá giá').click(function() { return false; });
                return;
            }
            doCountDownV3PP(id, $day, $hour, $min, $sec);
        }
    }
}

function doCountDownV3PP(id, _day, _hour, _min, _sec) {
    if (_sec > 0) {
        _sec = _sec - 1;
        convertPart('sec_' + id.toString() + '_', _sec);
        if (($timeLV == 1) && (_sec == 0)) {
            $timeLV = 0;

            $('.js_timecount' + id.toString()).parent().parent().find('.imgBtBuy').attr('src', '/images/groupon/buttons/buy_expire.jpg').attr('alt', 'Hết hạn phá giá').click(function() { return false; });
        };
        $timeout = window.setTimeout(function() { doCountDownV3PP(id, _day, _hour, _min, _sec); }, 1000); return;
    }
    if (_min > 0) {
        _min = _min - 1;
        _sec = 59;
        convertPart('sec_' + id.toString() + '_', _sec);
        convertPart('min_' + id.toString() + '_', _min);
        if (($timeLV == 2) && (_min == 0)) { convertPart('min_' + id.toString() + '_', 0); ; $timeLV = 1; };
        $timeout = window.setTimeout(function() { doCountDownV3PP(id, _day, _hour, _min, _sec); }, 1000); return;
    }
    if (_hour > 0) {
        _hour = _hour - 1;
        _min = 59;
        _sec = 59;
        convertPart('sec_' + id.toString() + '_', _sec);
        convertPart('min_' + id.toString() + '_', _min);
        convertPart('hour_' + id.toString() + '_', _hour);
        if (($timeLV == 3) && (_hour == 0)) { convertPart('hour_' + id.toString() + '_', 0); $timeLV = 2; };
        $timeout = window.setTimeout(function() { doCountDownV3PP(id, _day, _hour, _min, _sec); }, 1000); return;
    }
        if (_day > 0) {
        _day = _day - 1;
        _hour = 23;
        _min = 59;
        _sec = 59;
        convertPart('sec_' + id.toString() + '_', _sec);
        convertPart('min_' + id.toString() + '_', _min);
        convertPart('hour_' + id.toString() + '_', _hour);
        convertPart('day_' + id.toString() + '_', _day);
        if (($timeLV == 4) && (_day == 0)) { convertPart('day_' + id.toString() + '_', 0); $timeLV = 3; };
        $timeout = window.setTimeout(function() { doCountDownV3PP(id, _day, _hour, _min, _sec); }, 1000); return;
    }
}

function doCountDownV3(id) {
    $sec = parseInt($('.HD_seconds' + id.toString() + ' .timecount').text());
    if ($sec > 0) {
        $sec = $sec - 1;
        $('.HD_seconds' + id.toString() + ' .timecount').text($sec.toString());
        if (($timeLV == 1) && ($sec == 0)) {
            $timeLV = 0;
            $('.remaining_time_container' + id.toString() + ' p').text('Phá giá kết thúc vào:');
            $('.HD_seconds' + id.toString() + '').html($deadtime.getHours().toString() + ':' + $deadtime.getMinutes().toString() + '<br/>' + $deadtime.getDate().toString() + '/' + $deadtime.getMonth().toString() + '/' + $deadtime.getFullYear().toString());
            $('.remaining_time_container' + id.toString() + ' img').attr('src', '/images/groupon/icons/hourglass/hourglassfinal.gif');
            var hh = parseInt($deadtime.getHours().toString());
            var mm = parseInt($deadtime.getMinutes().toString());
            $('.HD_seconds' + id.toString() + '').html(((hh > 12) ? ((hh - 12).toString()) : hh.toString()) + ':' + (mm > 10 ? mm.toString() : '0' + mm.toString()) + (hh > 12 ? ' PM' : ' AM') + '<br/>' + $deadtime.getDate().toString() + '/' + ($deadtime.getMonth() + 1).toString() + '/' + $deadtime.getFullYear().toString());
            $('.remaining_time_container' + id.toString() + ' img').attr('src', '/images/groupon/icons/hourglass/hourglassfinal.gif');
            $('.remaining_time_container' + id.toString()).parent().find('.imgBtBuy').attr('src', '/images/groupon/buttons/buy_expire.jpg').attr('alt', 'Hết hạn phá giá').click(function() { return false; });
            $('div[class=for_a_friend]').hide();
        };
        $timeout = window.setTimeout(function() { doCountDownV3(id); }, 1000); return;
    }
    $min = parseInt($('.HD_minutes' + id.toString() + ' .timecount').text());
    if ($min > 0) {
        $min = $min - 1;
        $sec = 59;
        $('.HD_seconds' + id.toString() + ' .timecount').text($sec.toString());
        $('.HD_minutes' + id.toString() + ' .timecount').text($min.toString());
        if (($timeLV == 2) && ($min == 0)) { $('.HD_minutes' + id.toString() + '').hide(); $timeLV = 1; };
        $timeout = window.setTimeout(function() { doCountDownV3(id); }, 1000); return;
    }
    $hour = parseInt($('.HD_hours' + id.toString() + ' .timecount').text());
    if ($hour > 0) {
        $hour = $hour - 1;
        $min = 59;
        $sec = 59;
        $('.HD_seconds' + id.toString() + ' .timecount').text($sec.toString());
        $('.HD_minutes' + id.toString() + ' .timecount').text($min.toString());
        $('.HD_hours' + id.toString() + ' .timecount').text($hour.toString());
        if (($timeLV == 3) && ($hour == 0)) { $('.HD_hours').hide(); $timeLV = 2; };
        $timeout = window.setTimeout(function() { doCountDownV3(id); }, 1000); return;
    }
    $day = parseInt($('.HD_days' + id.toString() + ' .timecount').text());
    if ($day > 0) {
        $day = $day - 1;
        $hour = 23;
        $min = 59;
        $sec = 59;
        $('.HD_seconds' + id.toString() + ' .timecount').text($sec.toString());
        $('.HD_minutes' + id.toString() + ' .timecount').text($min.toString());
        $('.HD_hours' + id.toString() + ' .timecount').text($hour.toString());
        $('.HD_days' + id.toString() + ' .timecount').text($day.toString());
        if (($timeLV == 4) && ($day == 0)) { $('.HD_days' + id.toString() + '').hide(); $timeLV = 3; };
        $timeout = window.setTimeout(function() { doCountDownV3(id); }, 1000); return;
    }
}

function loadDetails(id) {
    var $obj = $('.dealstream' + id.toString());
    $('.pane-components').html(
        $.tmpl($('#detailTemplate').html().replace(/#%7B/g, '#{').replace(/%7D/g, '}'),
            { 'dealTitle': $obj.attr('dealtitle'),
                'dealUrl': $obj.attr('dealurl'),
                'discount_price': $obj.attr('dealdiscountprice'),
                'origin_price': $obj.attr('dealoriginprice'),
                'save_price': $obj.attr('dealsaveprice'),
                'dealImg': $obj.attr('dealimg'),
                'discountPercent': $obj.attr('dealdiscountpercent'),
                'condition_content': $obj.attr('dealcondition'),
                'highlight_content': $obj.attr('dealhighlight')
            }
        )
    )
}

function convertPart(part, value) {
    var str = value.toString();
    
    if (str.length > 1) {
        $('.' + part + '1').text(str.split("", 2)[1]);
        $('.' + part + '0').text(str.split("", 2)[0]);
    }
    else {
        $('.' + part + '1').text(str);
        $('.' + part + '0').text('0');
    }
}